package com.wooophone.test;

/**   
 * 内容摘要：花瓣网采集类  
 * 流程说明：  
 * @author haozi
 * @return   
 */  
 
import org.htmlparser.Node;  
import org.htmlparser.tags.ImageTag;  
import org.htmlparser.tags.LinkTag;  
 
 
public class CrawPicForHuaBan {  
    private static String baseUrl = "http://huaban.com";  
    //从首页采集包含图片的URL列表  
    public void crawImageList(String indexUrl, String encode) {  
        // 采集源码  
        String page = CrawNewsTools.getPage(indexUrl, encode);  
        System.out.println("###采集到首页的内容:" + page);  
        String templateNewsUrl = "/pins/(.*)";  
        Node[] nodes = CrawNewsTools.getTagList(page, encode, LinkTag.class);  
        for (int i = 0; i < nodes.length; i++) {  
            String link = ((LinkTag) nodes[i]).getLink();  
            boolean isRegex = CrawNewsTools.isRegex(link, templateNewsUrl);  
            if (isRegex) {  
                String completeLink = baseUrl + link;  
                System.out.println("###符合规则的URL：" + completeLink);  
                crawImage(completeLink, encode);  
            }  
        }  
 
    }  
    //采集图片  
    public void crawImage(String url, String encode) {  
        // 采集源码  
        String page = CrawNewsTools.getPage(url, encode);  
        String formatPage = CrawNewsTools.fromatBodyTag(page);  
        String bodyPage = CrawNewsTools.getBody(formatPage);  
        String body = CrawNewsTools.formatHtml(bodyPage);  
        System.out.println("###格式后body源码:\r\n" + body);  
        Node[] nodes = CrawNewsTools.getTagList(body, encode, ImageTag.class);  
        for (int i = 0; i < nodes.length; i++) {  
            String picUrl = ((ImageTag) nodes[i]).getImageURL();  
            String patternUrl = "http://img.hb.aicdn.com/(.*)";  
            boolean isRegex = CrawNewsTools.isRegex(picUrl, patternUrl);  
            if (isRegex) {  
                System.out.println("图片URL：" + picUrl);  
            }  
        }  
    }  
 
    public static void main(String[] args) {  
        String url = "http://huaban.com/all/beauty/";  
        String encode = "UTF-8";  
        new CrawPicForHuaBan().crawImageList(url, encode);  
    }  
 
}